package com.blog.service.Impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.blog.dao.TagMapper;
import com.blog.pojo.Tag;
import com.blog.service.TagService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Copyright (C), 2022
 * Author: 苏大大
 * Date: 2022-04-23 23:02
 * FileName: BlogTagServiceImpl
 * Description: 博客标签业务层实现
 */
@Service
public class TagServiceImpl implements TagService {
    @Resource
    private TagMapper mapper;
    @Resource
    private RedisTemplate redisTemplate;
    @Override
    public List<Tag> queryTagAll() {
        List<Tag> CacheList=mapper.queryTagAll();
        if(redisTemplate.opsForValue().get("TagList")!=null&&CacheList==redisTemplate.opsForValue().get("TagList")){
            String a=redisTemplate.opsForValue().get("TagList").toString();
            List<Tag> list=(List<Tag>) JSON.parseArray(a,Tag.class);
            return list;
        }
        if(CacheList!=null){
            //异常设备的json数组,把list转换为json形式数组
            JSONArray jsonArray= JSONObject.parseArray(JSONObject.toJSONString(CacheList));
            //将json形式数组装换为string类型
            String json= jsonArray.toJSONString();
            //将需要缓存的数据缓存入redis
            redisTemplate.opsForValue().set("TagList",json);
        }
        String a=redisTemplate.opsForValue().get("TagList").toString();
        List<Tag> List=(List<Tag>) JSON.parseArray(a,Tag.class);

        return List;
    }

    @Override
    public List<Tag> queryTagPageAll(String queryString) {
        return mapper.queryTagPageAll(queryString);
    }

    @Override
    public PageInfo<Tag> queryTagPageAll(int pageNumber, int pageSize,String queryString) {
        PageHelper.startPage(pageNumber,pageSize);
        List<Tag> List= mapper.queryTagPageAll(queryString);
        PageInfo<Tag> pageInfo=new PageInfo<>(List);
        return pageInfo;
    }
}
